//
//  LSEditIntroduceViewController.m
//  LookScore
//
//  Created by wr on 2018/5/7.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSEditIntroduceViewController.h"
#import "LSHttpEnclosureTool.h"
#import "LSAccountArchive.h"
#import "UITextView+Placeholder.h"
#import "MBProgressHUD+MJ.h"
@interface LSEditIntroduceViewController ()<UITextViewDelegate>
@end

@implementation LSEditIntroduceViewController{
    UILabel *countLable;
    UITextView *wordsText;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.translucent = YES;
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)initUI{
    
    self.view.backgroundColor=LSSubGrayColorf7;
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, 64)];
    navView.backgroundColor = LSNavMainColor;
    
    UIButton*rightButton = [[UIButton alloc]init];
    rightButton.titleLabel.font = LSFontSize13;
    [rightButton setTitle:@"完成" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(action_EditCompleted:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton*leftButton = [[UIButton alloc]init];
    leftButton.titleLabel.font = LSFontSize13;
    [leftButton setTitle:@"取消" forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(action_Cancel:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLable = [[UILabel alloc] init];
    titleLable.text = @"设置个性签名";
    titleLable.font = LSFontSize15;
    titleLable.textAlignment = NSTextAlignmentCenter;
    titleLable.textColor = LSNavTitlenColor;
    
    UIView *editView = [[UIView alloc] init];
    editView.backgroundColor = LSTitleWhiteColor;
    
    wordsText = [[UITextView alloc] init];
    wordsText.returnKeyType = UIReturnKeyDefault;
    wordsText.text=[LSAccountArchive account].SpecialWords;
    wordsText.placeholder=@"编辑您的个性签名...";
    [wordsText setTextColor:[UIColor blackColor]];
    wordsText.placeholderColor=Color(148, 148, 148);
    wordsText.delegate=self;
    
    countLable = [[UILabel alloc] init];
    countLable.text = [NSString stringWithFormat:@"%zd/45",wordsText.text.length];
    countLable.textAlignment =  NSTextAlignmentRight;
    countLable.textColor = LSTitleGrayColor3;
    countLable.font = LSFontSize13;
    
    
    [self.view addSubview:navView];
    [self.view addSubview:editView];
    [navView addSubview:rightButton];
    [navView addSubview:leftButton];
    [navView addSubview:titleLable];
    [editView addSubview:countLable];
    [editView addSubview:wordsText];
    
    leftButton.sd_layout.leftEqualToView(navView).offset(10).bottomEqualToView(navView).offset(-10).widthIs(50).heightIs(20);
    rightButton.sd_layout.rightEqualToView(navView).offset(-10).bottomEqualToView(navView).offset(-10).widthIs(50).heightIs(20);
    titleLable.sd_layout.centerXEqualToView(navView).bottomEqualToView(navView).offset(-10).widthIs(100).heightIs(20);
    editView.sd_layout.leftEqualToView(navView).rightEqualToView(navView).topSpaceToView(navView, 20).heightIs(100);
    countLable.sd_layout.rightEqualToView(editView).offset(-10).bottomEqualToView(editView).offset(-10).widthIs(100).heightIs(20);
    wordsText.sd_layout.leftEqualToView(editView).offset(10).rightEqualToView(editView).offset(-10).topEqualToView(editView).offset(10).bottomEqualToView(editView).offset(-35);
}

#pragma mark 🔥 UITextViewDelegate

- (void)textViewDidChange:(UITextView *)textView{
    
    NSString *inputText=textView.text;
    if (textView.text.length <= 45) {
        countLable.text = [NSString stringWithFormat:@"%zd/%d",inputText.length,45];
    }
    else{
        countLable.text = @"45/45";
        NSString *msg=[NSString stringWithFormat:@"字数不能超过%d",45];
        [MBProgressHUD showOnlyMessage:msg afterDelay:1.5f];
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 45)];
        [textView.undoManager removeAllActions];
        [textView becomeFirstResponder];
        
        return;
    }
}
//- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
//
////    NSString *inputString=[textView.text stringByReplacingCharactersInRange:range withString:text];
////
////    if (inputString.length>45) {
////
////        NSString *msg=[NSString stringWithFormat:@"字数不能超过%d",45];
////        [MBProgressHUD showError:msg toView:self.view];
////        return NO;
////    }else{
////        return YES;
////    }

//}


#pragma mark 🔥 Button点击事件
- (void)action_EditCompleted:(UIButton *)sender{
//    MyLog(@"emoj --- %@",wordsText.text);
//    NSString *str = [wordsText.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    MyLog(@"str --- %@",str);
    [LSHttpEnclosureTool POST:LSUserChangeWords parameters:@{@"cid":[LSAccountArchive account].Cid,@"words":wordsText.text} resBlock:^(id responseObject) {
        if (responseObject) {
            LSAccountCoder *coderModel = [LSAccountArchive account];
            coderModel.SpecialWords=wordsText.text;
            [LSAccountArchive saveAccount:coderModel];
//            [LSAccountArchive account].SpecialWords = wordsText.text;
            if (self.PersonalDataChangedBlock) {
                self.PersonalDataChangedBlock();
            }
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }];
}
- (void)action_Cancel:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
